import mimetypes
import os

import sqlalchemy
from fastapi import FastAPI
from fastapi.responses import FileResponse, Response
import uvicorn
from api.utils.get_config import config_data

from fastapi import Request

from api.api_model.response_model import ApiResponse

app = FastAPI()

from api.midware.midware import auth_midware


@app.middleware("http")
async def auth_middleware(request: Request, call_next):
    response = await auth_midware(request, call_next, engine)
    return response


DATABASE_URL = config_data['mysql']
engine = sqlalchemy.create_engine(DATABASE_URL)


@app.get("/smart_class/ping")
async def ping():
    # http://127.0.0.1:8001/smart_class/ping
    return {"ping": "pong"}
from api.my_grade import get_semesters_api
@app.get("/smart_class/semester")
async def ping():
    response = await get_semesters_api(engine)
    # http://127.0.0.1:8001/smart_class/semester
    return response

STATIC_FOLDER = "tmp_imgs"
STATIC_PATH = f"/{STATIC_FOLDER}"
# http://127.0.0.1:8001/tmp_imgs/mlkjcvep.png
@app.get(f"/{STATIC_FOLDER}/{{filename}}")
async def read_static_file(request: Request, filename: str):
    filepath = os.path.join(STATIC_FOLDER, filename)
    if os.path.isfile(filepath):
        # 猜测文件的MIME类型
        content_type, _ = mimetypes.guess_type(filepath)
        if content_type is None:
            content_type = "application/octet-stream"  # 默认为二进制流，如果无法确定类型
        # 读取文件内容
        with open(filepath, "rb") as file:
            file_content = file.read()
        # 返回Response对象，文件内容作为字节流发送
        return Response(content=file_content, media_type=content_type)
    else:
        return {"error": "File not found"}


from api.login import UserRegisterModel, register_api


@app.post("/api/register", response_model=ApiResponse)
async def register(request: Request, data: UserRegisterModel):
    response = await register_api(request, data, engine)
    return response


from api.login import UserLoginModel, login_api


@app.post("/api/login/")
async def login(request: Request, data: UserLoginModel):
    response = await login_api(request, data, engine)
    return response


from api.login import UserOpenidModel, set_openid_api


@app.post("/api/set-openid/")
async def login(request: Request, data: UserOpenidModel):
    response = await set_openid_api(request, data, engine)
    return response


from api.login import ChangePwdModel, change_pwd_api


@app.post("/api/change-pwd/")
async def change_pwd(request: Request, data: ChangePwdModel):
    response = await change_pwd_api(request, data, engine)
    return response



from api.user import get_user_info_api


@app.post("/api/userinfo/{uid}")
async def get_userinfo(request: Request, uid: str):
    response = await get_user_info_api(request, uid, engine)
    return response


from api.my_class import get_class_list_api


@app.post("/api/my-class/{class_name}")
async def get_my_class(request: Request, class_name: str):
    response = await get_class_list_api(request, class_name, engine)
    return response


from api.teacher import get_teacher_class_api


@app.post("/api/teacher-class/{uid}")
async def get_teacher_class(request: Request, uid: str):
    response = await get_teacher_class_api(request, uid, engine)
    return response


from api.my_grade import get_my_grade_list_api


@app.post("/api/stu-grade/{uid}")
async def get_my_grade_list(request: Request, uid: str):
    response = await get_my_grade_list_api(request, uid, engine)
    return response


from api.my_grade import get_class_grade_list_api


@app.post("/api/class-grade/{class_name}")
async def get_class_grade_list(request: Request, class_name: str):
    response = await get_class_grade_list_api(request, class_name, engine)
    return response


from api.my_grade import get_lesson_grade_list_api


@app.post("/api/lesson-grade/{class_name}")
async def get_lesson_grade_list(request: Request, class_name: str):
    response = await get_lesson_grade_list_api(request, class_name, engine)
    return response


from api.policy import get_policy_list_api


@app.post("/api/policy-list/")
async def get_policy_list(request: Request):
    response = await get_policy_list_api(request, engine)
    return response


from api.policy import get_policy_detail_api


@app.post("/api/policy/{policy_name}")
async def get_policy_list(request: Request, policy_name: str):
    response = await get_policy_detail_api(request, policy_name, engine)
    return response


from api.prediction import class_grade_prediction_api, ClassGradePredictionModel


@app.post("/api/class-grade-prediction/")
async def class_grade_prediction(request: Request, data: ClassGradePredictionModel):
    response = await class_grade_prediction_api(request, data,engine)
    return response


from api.prediction import check_predictable_lessons_api


@app.post("/api/check-predictable-lessons/")
async def check_predictable_lessons(request: Request):
    response = await check_predictable_lessons_api(request, engine)
    return response


from api.group import class_group_api, ClassGroupModel


@app.post("/api/class-group/")
async def class_group(request: Request, data: ClassGroupModel):
    response = await class_group_api(request, data, engine)
    return response


from api.announcement import get_announcement_list_api


@app.post("/api/announcement-list/{class_name}")
async def announcement_list(request: Request,  class_name: str):
    response = await get_announcement_list_api(request, class_name, engine)
    return response

from api.announcement import send_announcement_api, SendAnnouncementModel

@app.post("/api/send-announcement/")
async def announcement_list(request: Request,  data: SendAnnouncementModel):
    response = await send_announcement_api(request, data, engine)
    return response


from api.message import get_message_list_api


@app.post("/api/message-list/{uid}")
async def message_list(request: Request,  uid: str):
    response = await get_message_list_api(request, uid, engine)
    return response


from api.message import send_message_api, SendMessageModel


@app.post("/api/send-message/")
async def send_message(request: Request,  data: SendMessageModel):
    response = await send_message_api(request, data, engine)
    return response


from api.message import send_class_message_api, SendClassMessageModel


@app.post("/api/send-class-message/")
async def send_class_message(request: Request,  data: SendClassMessageModel):
    response = await send_class_message_api(request, data, engine)
    return response


from api.ai_agent import ai_agent_api, AgentInput
@app.post("/api/ai-agent/")
async def ai_agent(request: Request, user_input: AgentInput):
    response = await ai_agent_api(request, user_input, config_data['agent_url'])
    return response


if __name__ == "__main__":
    uvicorn.run(app, host=config_data['server_host'], port=config_data['server_port'])

# 计算机专业核心课程有哪些
#
# 查询计211的每个同学的平均成绩，画出前三名的成绩对比图
#
# 我的大学物理会挂科吗
# 我是张伟
